<?php
namespace Modules\ShopAdmin\Http\Controllers;
use Illuminate\Http\Request;
use Modules\Admin\Http\Requests\CommonStatusRequest;
use Modules\Admin\Http\Requests\CommonSortRequest;
use Modules\Admin\Http\Requests\CommonDeleteRequest;
use Modules\Admin\Http\Requests\CommonDeleteAllRequest;
use Modules\ShopAdmin\Models\ShopProductType as ShopProductTypeModel;
use Modules\ShopAdmin\Models\ShopProduct as ShopProductModel;
use Modules\ShopAdmin\Models\ShopProductAttrbuteValue as ShopProductAttrbuteValueModel;
use Modules\ShopAdmin\Models\ShopProductAttrbuteKey as ShopProductAttrbuteKeyModel;
use Modules\ShopAdmin\Models\ShopProductComment as ShopProductCommentModel;
use Modules\Admin\Models\Image as ImageModel;
class ProductController extends CommonShopController
{
	/**
	 * @name  获取属性 
	 */
    public function attrbute_key($id)
    { 
		return ShopProductAttrbuteKeyModel::where(['product_type_id'=>$id])->orderBy('sort','asc')->orderBy('id','desc')->select('id','name')->get();
    }
	/**
	 * @name  获取属性值 
	 */
    public function attrbute_value($ids)
    {
		return ShopProductAttrbuteKeyModel::whereIn('id',explode(',',$ids))
				->with('attrbute_value_to')
				->orderBy('sort','asc')->orderBy('id','desc')
				->select('id','name')->get()->toArray();
    }
	/**
	 * @name  添加属性
	 */
    public function attrbute_key_store(Request $request)
    {
		$data = $request->all();
		$data['created_at'] = date('Y-m-d H:i:s');
		if ($id = (new ShopProductAttrbuteKeyModel())->insertGetId($data)){
			return response()->json([
				'status'=>1,
				'error' => '',
				'msg' => $id
			],200);
        }else{
			return response()->json([
				'status'=>0,
				'error' => config('admin.create_error'),
				'msg' => ''
			],201);
		} 
    }
	/**
	 * @name  添加属性值
	 */
    public function attrbute_value_store(Request $request)
    {
		$data = $request->all();
		$data['created_at'] = date('Y-m-d H:i:s');
		if ($id = (new ShopProductAttrbuteValueModel())->insertGetId($data)){
			return response()->json([
				'status'=>1,
				'error' => '',
				'msg' => $id
			],200);
        }else{
			return response()->json([
				'status'=>0,
				'error' => config('admin.create_error'),
				'msg' => ''
			],201);
		} 
    }
	/**
	 * @name  列表
	 */
    public function index()
    {  
		$status = ShopProductModel::product_status();
		$recommend_status = ShopProductModel::recommend_status();
		$best_status = ShopProductModel::best_status();
        return view('shopadmin::product.index',compact('status','recommend_status','best_status'));
    }
	/**
	 * @name  列表数据
	 */ 
    public function data(Request $request)
    {
	    $model = ShopProductModel::query();
		$params = $request->all();
		$model = query_condition($model,$params,'name'); 
		if(isset($params['recommend_status']) && $params['recommend_status'] != ''){
			$model = $model->where('recommend_status',$params['recommend_status']);
		}
		if(isset($params['best_status']) && $params['best_status'] != ''){
			$model = $model->where('best_status',$params['best_status']);
		}
        $res = $model->orderBy('sort','asc')->orderBy('id','desc')
				->with('product_type_to','image_one','product_spec_to')
				->withCount('comments')
				->paginate($params['limit'])
				->toArray();
		$res['data'] = ShopProductModel::product_list($res['data']);		
        return response()->json(['code' => 0, 'msg'   => config('admin.limit_msg'),'count' => $res['total'],'data'  => $res['data'],'rel'=>1]);
    }
	/**
	 * @name  添加页面
	 */
    public function create()
    { 
		$product_type_info = (new ShopProductTypeModel)->lefts();
		return view('shopadmin::product.create',compact('product_type_info'));
    }
	/**
	 * @name  添加数据处理 
	 */
    public function store(Request $request)
    {
		$data = $request->all();
		$ImageModel = new ImageModel();
		$data = $ImageModel->add_image_all($data,'images','file');
		if(!is_array($data)){
			return $data;
		};
		$data = $ImageModel->add_image($data,'file');
		if(!is_array($data)){
			return $data;
		};	
		return (new ShopProductModel())->product_add($data,route('shop_admin.product.index'));  
    }
	/**
	 * @name  编辑页面
	 */
    public function edit($id)
    {
		$info = ShopProductModel::with('image_one','product_spec_to')->find($id)->toArray();
		$info['type_title'] = ShopProductTypeModel::where(['id'=>$info['product_type_id']])->value('title');
		if($info['specs_status'] == 1){
			$info['attrbute_list'] = json_decode($info['attrbute_list'],true);
			$attrbute_list = array_column($info['attrbute_list'],'name');
			foreach($info['product_spec_to'] as $k=>$v){
				$info['product_spec_to'][$k]['specs'] = json_decode($v['specs'],true);
			}
		}else{
			$attrbute_list = [];
		}
		if($info['images']){
			$images = (new ImageModel())->whereIn('id',explode('|',$info['images']))->get()->toArray();
		}else{
			$images = [];
		}
		$attrbute_list_count = count($attrbute_list);
		return view('shopadmin::product.edit',compact('info','attrbute_list','attrbute_list_count','images'));
    }
	/**
	 * @name  编辑数据处理
	 */
    public function update(Request $request,$id)
    {
		$data = $request->all();
		$ImageModel = new ImageModel(); 
		$data = $ImageModel->edit_image('shop_products',$id,$data,'file');
		if(!is_array($data)){
			return $data;
		}; 
		$data = $ImageModel->edit_image_all('shop_products',$id,$data,'images','file');
		if(!is_array($data)){
			return $data;
		}; 
		return (new ShopProductModel())->product_update($data,$id,route('shop_admin.product.index')); 
    }
	/**
	 * @name 状态数据处理
	 */
    public function status(CommonStatusRequest $request,$id)
    {
		return (new ShopProductModel())->common_status_update($request->all(),$id,route('shop_admin.product.index')); 
    }
	/**
	 * @name 是否精品推荐数据处理
	 */
    public function recommend_status(Request $request,$id)
    {
		return (new ShopProductModel())->common_status_update($request->all(),$id,route('shop_admin.product.index')); 
    }
	/**
	 * @name 是否热卖单品数据处理
	 */
    public function best_status(Request $request,$id)
    {
		return (new ShopProductModel())->common_status_update($request->all(),$id,route('shop_admin.product.index')); 
    }
	/**
	 * @name  排序数据处理
	 */
    public function sorts(CommonSortRequest $request,$id)
    {
		return (new ShopProductModel())->common_status_update($request->all(),$id,route('shop_admin.product.index'));  
    }
	/**
	 * @name  删除数据处理
	 */
    public function destroy(CommonDeleteRequest $request)
    {	
		$ImageModel = new ImageModel();
		$id = $ImageModel->del_image('shop_products',$request->get('id'));
		$id = $ImageModel->del_image_all('shop_products',$id,'images');
		if(!is_numeric($id)){
			return $id;
		};
		return (new ShopProductModel())->product_destroy($id,route('shop_admin.product.index'));  
    }
	
	/**
	 * @name  评论列表
	 */
    public function comments_index(Request $request)
    { 
		$id = $request->get('id');
		$name = ShopProductModel::where(['id'=>$id])->value('name');
        return view('shopadmin::product.comments_index',compact('id','name'));
    }
	/**
	 * @name  评论列表数据
	 */
    public function comments_data(Request $request)
    {
	    $model = ShopProductCommentModel::query();
		$params = $request->all();
		$model = query_condition($model,$params); 
		$model = $model->where('product_id',$params['id']);
        $res = $model
				->with([
					'user_to'=>function($query){
						$query->select('id','phone','nick_name');
					}
				])
				->orderBy('id','desc')  
				->paginate($params['limit'])
				->toArray();			
		$res['data'] = (new ShopProductCommentModel())->formatting_list($res['data']);	
        return response()->json(['code' => 0, 'msg'   => config('admin.limit_msg'),'count' => $res['total'],'data'  => $res['data'],'rel'=>1]);
    }
	/**
	 * @name  评论列表单个删除
	 */
    public function comments_destroy(CommonDeleteRequest $request)
    {
		$id = (new ImageModel())->del_image_all('shop_product_comments',$request->get('id'),'images');
		if(!is_numeric($id)){
			return $id;
		};
		return (new ShopProductCommentModel())->common_destroy($id,route('shop_admin.product.comments_index',['id'=>$request->get('product_id')]));  
    }
	/**
	 * @name  评论列表批量删除数据处理
	 */
    public function comments_destroy_all(CommonDeleteAllRequest $request)
    {
		$ids = (new ImageModel())->del_all_images('shop_product_comments',$request->get('ids'),'images');
		if(!is_array($ids)){
			return $ids;
		};
		return (new ShopProductCommentModel())->common_destroy($ids,route('shop_admin.product.comments_index',['id'=>$request->get('product_id')]));  
    }
	
}
